<?php

namespace controller;

/**
 * Description of ControllerBase
 *
 * @author Damian
 */
$hosta = \library\RequestMethods::server("HTTP_HOST");
switch ($hosta) {
    case (strcmp($hosta, "localhost:8080")==0): {
            define("defaultPath","\\application\\view\\standard");
            define("defaultLayout","\\layout\\standard");
            break;
        }
        default : {
            define("defaultPath","application/view/standard");
            define("defaultLayout","/layout/standard");
            break;
        }
}

class ControllerBase extends \library\Base {

    /**
     *
     * @readwrite
     */
    protected $parameters;

    /**
     * @readwrite
     * @var \view\View 
     */
    protected $layoutView;

    /**
     * @readwrite
     * @var \view\View 
     */
    protected $actionView;

    /**
     * @readwrite
     */
    protected $willRenderLayoutView = true;

    /**
     * @readwrite
     */
    protected $willRenderActionView = true;

    /**
     * @readwrite
     */
    protected $defaultPath = defaultPath;

    /**
     * @readwrite
     */
    protected $defaultLayout = defaultLayout;

    /**
     * @readwrite
     */
    protected $defaultExtension = "html";

    /**
     * @readwrite
     */
    protected $defaultContentType = "text/html";

    protected function _getExceptionForArgument() {
        return new exceptions\WrongArgumentException();
    }

    /**
     * 
     * @return \view\View 
     */
    public function getActionView() {
        return $this->actionView;
    }

    public function render() {
        $defaultContentType = $this->getDefaultContentType();
        $results = null;
        $doAction = $this->getWillRenderActionView() && $this->getActionView();
        $doLayout = $this->getWillRenderLayoutView() && $this->getLayoutView();
      //  try {
            if ($doAction) {
                $view = $this->getActionView();
                $results = $view->render();
            }
            if ($doLayout) {
                $view = $this->getLayoutView();
                $view->set("template", $results);
                $results = $view->render();
            }
            if ($doAction) {

                if (!headers_sent()) {
                    header("Content-type: {$defaultContentType}");
                }
                echo $results;

                $this->setWillRenderLayoutView(false);
                $this->setWillRenderActionView(false);
            }
//        } catch (\Exception $e) {
//            //  echo $e;
//            throw new \view\exceptions\RendererException("nieprawidłowa składnia układu/szablonu: " . $e->getMessage());
//        }
    }

    public function __destruct() {
        // $this->render();
    }

    public function __construct($options = array()) {
        parent::__construct($options);
        $configuration = \library\Registry::get("configuration");
        $host = \library\RequestMethods::server("HTTP_HOST");

        switch ($host) {
            case "localhost:8080": {
                    $host = "default";
                    break;
                }
            case "alequizdev.pl": {
                    $host = "default";
                    break;
                }
        }
        if ($this->getWillRenderLayoutView()) {
            $defaultPath = $this->getDefaultPath();
            $defaultLayout = $this->getDefaultLayout();
            $defaultExtension = $this->getDefaultExtension();
            if ($host == "localhost:8080") {
                $view = new \view\View(array(                    
                    "file" => APP_PATH . "\\{$defaultPath}\\{$defaultLayout}.{$defaultExtension}"));
            } else {
                $view = new \view\View(array(
                    "file" => APP_PATH . "/{$defaultPath}/{$defaultLayout}.{$defaultExtension}"));
            }

            $this->setLayoutView($view);
        }
        if ($this->getWillRenderActionView()) {

            $router = \library\Registry::get("router");
            /* @var $router \library\Router\Router */
            $controller = $router->getController();
            $action = $router->getAction();
            if ($host == "localhost:8080") {
                $view = new \view\View(array(
                    "file" => APP_PATH . "\\{$defaultPath}\\".strtolower($controller)."\\".strtolower($action).".".$defaultExtension
                ));
            } else {
                $view = new \view\View(array(
                    "file" => APP_PATH . "/{$defaultPath}/".strtolower($controller)."/".strtolower($action).".".$defaultExtension
                ));
            }

            //  var_dump($view);
            $this->setActionView($view);
        }
    }

}
